<?php
namespace Home\Model;

use Frame\Libs\BaseModel;

final class ArticleModel extends BaseModel
{
    protected $table = "article";

    public function fetchAllMonth()
    {
        //文章按时间归档方法
        $sql = "select date_format(from_unixtime(created_at),\"%Y年%m月\") as months,count(id) as records from article ";
        $sql .= "group by months";
        return $this->pdo->fetchAll($sql);
    }
    public function fetchAllArticle($where="2>1",$page=0,$pagesize=10,$order="asc") //
    {
        //获取多条文章连表数据
        $sql = "select article.*,user.name,category.classname from {$this->table} ";
        $sql .= "left join category on article.category_id=category.id ";
        $sql .= "left join user on article.user_id=user.id ";
        $sql .= "where {$where} ";
        $sql .= "order by article.id {$order} ";
        $sql .= "limit {$page},{$pagesize}";
        return $this->pdo->fetchAll($sql);
    }
    public function fetchOneArticle($where="2>1",$order="article.id asc")
    {
        //获取单条文章连表数据 上一篇 下一篇
        $sql = "select article.*,user.name,category.classname from {$this->table} ";
        $sql .= "left join category on article.category_id=category.id ";
        $sql .= "left join user on article.user_id=user.id ";
        $sql .= "where {$where} ";
        $sql .= "order by {$order}";
        //执行sql语句 返回一维数组
        return $this->pdo->fetchOne($sql);
    }
    public function updateRead($id)
    {
        //更新阅读数
        $sql = "update {$this->table} set `READ` = `READ`+1 where id={$id}";
        return $this->pdo->exec($sql);
    }
    public function updatePraise($id)
    {
        //更新点赞数量
        $sql = "update {$this->table} set `praise` = `praise`+1 where id={$id}";
        return $this->pdo->exec($sql);
    }
}